package com.jarvis.hospital.patient;


import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jarvis.bottomnavigationviewlibrary.BottomNavigationItem;
import com.jarvis.bottomnavigationviewlibrary.BottomNavigationView;
import com.jarvis.bottomnavigationviewlibrary.OnBottomNavigationItemClickListener;
import com.jarvis.hospital.R;
import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.login.model.LoginModel;
import com.jarvis.hospital.login.model.UserModel;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

import butterknife.BindView;


public class MainPatientActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.lin_message)
    LinearLayout lin_message;
    @BindView(R.id.img_messgae)
    ImageView img_messgae;
    @BindView(R.id.tv_message)
    TextView tv_message;
    @BindView(R.id.lin_task)
    LinearLayout lin_task;
    @BindView(R.id.img_task)
    ImageView img_task;
    @BindView(R.id.tv_task)
    TextView tv_task;
    @BindView(R.id.lin_patient)
    LinearLayout lin_patient;
    @BindView(R.id.img_patient)
    ImageView img_patient;
    @BindView(R.id.tv_patient)
    TextView tv_patient;

    private PatientMessageFragment patientMessageFragment;
    private PatientManageFragment patientManageFragment;
    private PatientTaskFragment patientTaskFragment;
    Fragment mContent;



    @Override
    public int getLayoutId() {
        return R.layout.activity_patient;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        lin_message.setOnClickListener(this);
        lin_patient.setOnClickListener(this);
        lin_task.setOnClickListener(this);

        Bundle bundle = this.getIntent().getExtras();
        String type = bundle.getString("type");
        if("message".equals(type)){
            changeFrgment(1);
            patientMessageFragment = new PatientMessageFragment();
            changeFragment(R.id.fl_main, patientMessageFragment);
        }else  if("brgl".equals(type)){
            changeFrgment(2);
            patientManageFragment = new PatientManageFragment();
            changeFragment(R.id.fl_main, patientManageFragment);
        }else {
            changeFrgment(0);
            patientTaskFragment = new PatientTaskFragment();
            changeFragment(R.id.fl_main, patientTaskFragment);
        }
        boolean isAuto = bundle.getBoolean("isAuto");
        if (isAuto) {
//            UpdateManager manager = new UpdateManager(this);
//            manager.checkUpdate();
        }
        UserModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        String userRole = userModel.getUserRole();
        if("patient".equals(userRole)){
            lin_task.setVisibility(View.VISIBLE);
        }
        //getPermission();
    }
    private void changeFrgment(int frg) {
        switch (frg) {
            case 0:
                img_task.setImageResource(R.drawable.img_message_1);
                tv_task.setTextColor(getResources().getColor(R.color.blue_nomal));
                img_messgae.setImageResource(R.drawable.img_message);
                tv_message.setTextColor(getResources().getColor(R.color.gray_2));
                img_patient.setImageResource(R.drawable.img_patient);
                tv_patient.setTextColor(getResources().getColor(R.color.gray_2));
                break;
            case 1:
                img_task.setImageResource(R.drawable.img_message);
                tv_task.setTextColor(getResources().getColor(R.color.gray_2));
                img_messgae.setImageResource(R.drawable.img_message_1);
                tv_message.setTextColor(getResources().getColor(R.color.blue_nomal));
                img_patient.setImageResource(R.drawable.img_patient);
                tv_patient.setTextColor(getResources().getColor(R.color.gray_2));
                break;
            case 2:
                img_task.setImageResource(R.drawable.img_message);
                tv_task.setTextColor(getResources().getColor(R.color.gray_2));
                img_messgae.setImageResource(R.drawable.img_message);
                tv_message.setTextColor(getResources().getColor(R.color.gray_2));
                img_patient.setImageResource(R.drawable.img_patient_1);
                tv_patient.setTextColor(getResources().getColor(R.color.blue_nomal));
                break;
        }
    }
    public void getPermission() {
        RxPermissions rxPermission = new RxPermissions(MainPatientActivity.this);
        rxPermission
                .requestEach(Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 用户已经同意该权限
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                            showTextToast("请选择允许使用权限");
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』
                            showTextToast("请选择允许使用权限");
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_task:
                changeFrgment(0);
                if (patientTaskFragment == null) {
                    patientTaskFragment = new PatientTaskFragment();
                }
                changeFragment(R.id.fl_main, patientTaskFragment);
                break;
            case R.id.lin_message:
                changeFrgment(1);
                if (patientMessageFragment == null) {
                    patientMessageFragment = new PatientMessageFragment();
                }
                changeFragment(R.id.fl_main, patientMessageFragment);
                break;
            case R.id.lin_patient:
                changeFrgment(2);
                if (patientManageFragment == null) {
                    patientManageFragment = new PatientManageFragment();
                }
                changeFragment(R.id.fl_main, patientManageFragment);
                break;

        }
    }
}
